package cn.itcast.web.controller.cargo;


import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingListService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2021-01-28
 */
@Controller
@RequestMapping("/cargo/packing")
public class PackingListController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private PackingListService packingListService;

    @Reference
    private ExportService exportService;


    @RequestMapping("/save.do")
    public String save(PackingList packingList){
        //将companyId放入packingList
        packingList.setCompanyId(getLoginUserCompanyId());
        //将packingList交由packingListService处理
        packingListService.savePacking(packingList);
        //跳转到装箱页面
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("list.do")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //获取companyId
        String loginUserCompanyId = getLoginUserCompanyId();
        //根据companyId查询该公司所有装箱信息
        PageInfo<PackingList> pageInfo = packingListService.findByPage(pageNum,pageSize,loginUserCompanyId);

        //保存到请求域
        request.setAttribute("pageInfo",pageInfo);

        //返回到list页面
        return "cargo/packing/packing-list";
    }

//    /cargo/packing/toView.do?id="+id
    @RequestMapping("/toView.do")
    public String toView(@RequestParam("id") String packingListId){
        PackingList packingList = packingListService.getById(packingListId);

        request.setAttribute("packing",packingList);

        return "cargo/packing/packing-view";
    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam("id") String packingListId){
        ResultInfo resultInfo = packingListService.delete(packingListId);

        if (resultInfo.getSuccess()){
            return "redirect:/cargo/packing/list.do";
        }else {
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }
    }

    @RequestMapping("/shipping.do")
    public String shipping(@RequestParam("id") String packingListId){
        ResultInfo resultInfo = packingListService.shipping(packingListId);

        if (resultInfo.getSuccess()){
            //若结果为true，data存储到请求域中
            request.setAttribute("shipping", ((List<Object>) resultInfo.getData()).get(0));
            request.setAttribute("marks", ((List<Object>) resultInfo.getData()).get(1));

            //跳转到添加委托信息页
            return "cargo/packing/packing-shipping";
        }else {
            //若结果为fasle，跳转到失败页
            request.setAttribute("error",resultInfo.getMsg());
            return "forward:/error-info.jsp";
        }
    }
}


